﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.IO;
using Pub;
using Raize.CodeSiteLogging;

namespace JCNT.DAL
{
    public class CPic
    {
        CSQLServer ado1 = new CSQLServer();
        public Cwp_pic wp_pic = new Cwp_pic();
        public Cwp_pic[] wp_pics;

        /// <summary>
        /// 填充树目录
        /// </summary>
        /// <param name="tv1"></param>
        /// <param name="node"></param>
        /// <param name="RootID"></param>
        public void fillTree(TreeView tv1, TreeNode node, string RootID)
        {
            string strSQL = "Select * From WP_PLab Where vis=1 and RootID='" + RootID + "' Order By Lab";
            DataSet ds1 = ado1.sel1(strSQL);
            //Pub.bugMsbox("RootID=" + RootID.ToString() + " And ds_count=" + ado1.ds_count.ToString());

            foreach (DataRow dr in ds1.Tables[0].Rows)
            {
                TreeNode node1 = new TreeNode();
                string Lab = dr["Lab"].ToString();
                string ID = dr["ID"].ToString();
                node1.Text = Lab;
                node1.Tag = ID;
                node1.ImageIndex = 0;

                fillTree(null, node1, ID);

                string strSQL2 = "Select ID, Nam, RootID From WP_Pic Where RootID = " + ID + " Order By Nam";
                DataSet ds2 = ado1.sel1(strSQL2);
                foreach (DataRow dr2 in ds2.Tables[0].Rows)
                {
                    TreeNode node2 = new TreeNode();
                    string Tit = dr2["Nam"].ToString();
                    string tID = dr2["ID"].ToString();
                    node2.Text = Tit;
                    node2.Tag = tID;
                    node2.ImageIndex = 1;
                    //node2.NavigateUrl = "Right.aspx?id=" + tID;
                    //node2.Target = "iframe1";
                    //node2.ImageUrl = @"\images\file.gif";
                    //node1.ChildNodes.Add(node2);
                    node1.Nodes.Add(node2);
                }

                if (RootID == "0")
                {
                    tv1.Nodes.Add(node1);
                }
                else
                {
                    node.Nodes.Add(node1);
                }
            }

        }

        public Image getPic(string ID)
        {
            string strSQL = "select * from wp_pic where ID=" + ID;
            ado1.sel1(strSQL);
            wp_pic.ID = ado1.ds.Tables[0].Rows[0]["ID"].ToString();
            wp_pic.Nam = ado1.ds.Tables[0].Rows[0]["Nam"].ToString();
            wp_pic.Lab = ado1.ds.Tables[0].Rows[0]["Lab"].ToString();
            wp_pic.Oth = ado1.ds.Tables[0].Rows[0]["Oth"].ToString();
            wp_pic.RootID = ado1.ds.Tables[0].Rows[0]["RootID"].ToString();
            wp_pic.Pic = ado1.drImg("Pic");
            return wp_pic.Pic;
        }

        public Cwp_pic[] getPics(string ID)
        {
            string strSQL = "select * from wp_pic where RootID=" + ID;
            DataSet ds1 = ado1.sel1(strSQL);
            wp_pics = new Cwp_pic[ado1.ds_count];
            int i = 0;
            foreach (DataRow dr1 in ds1.Tables[0].Rows)
            {
                wp_pics[i] = new Cwp_pic();
                wp_pics[i].ID = dr1["ID"].ToString();
                wp_pics[i].Nam = dr1["Nam"].ToString();
                wp_pics[i].Lab = dr1["Lab"].ToString();
                wp_pics[i].Oth = dr1["Oth"].ToString();
                wp_pics[i].RootID = dr1["RootID"].ToString();
                try
                {
                    wp_pics[i].Pic_Ico = CPub.btyeToImage((byte[])dr1["Pic_Ico"]);
                }
                catch (System.Exception ex)
                {
                    wp_pics[i].Pic_Ico = null;
                    //CPub.Send("Pic ID=" + wp_pics[i].ID + "; " + ex.ToString());
                }

                i++;
            }
            return wp_pics;
        }

        public String[] fillLv(ListView lv1, ImageList imglst1, string ID)
        {
            String[] ID_Lst;
            Cwp_pic[] wp_pics = getPics(ID);
            lv1.Items.Clear();
            imglst1.Images.Clear();
            ID_Lst = new String[wp_pics.Length];
            for (int i = 0; i < wp_pics.Length; i++)
            {
                if (wp_pics[i].Pic_Ico != null)
                    imglst1.Images.Add(wp_pics[i].Pic_Ico);
                ListViewItem item1 = new ListViewItem();
                item1.Text = wp_pics[i].Nam;
                item1.Tag = wp_pics[i].ID;
                item1.ImageIndex = i;
                lv1.Items.Add(item1);
                ID_Lst[i] = wp_pics[i].ID;
            }
            return ID_Lst;
        }

        public void setIco(String ID)
        {
            try
            {
                Image img = getPic(ID);

                int width = 128;
                int height = 128;
                if (img.Width > img.Height) height = width * img.Height / img.Width;
                else width = height * img.Width / img.Height;

                Bitmap bmp = new Bitmap(width, height);
                Graphics grap = Graphics.FromImage(bmp);
                grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                grap.DrawImage(img, new Rectangle(0, 0, width, height));

                Image img1 = bmp;
                MemoryStream stream1 = new MemoryStream();
                img1.Save(stream1, System.Drawing.Imaging.ImageFormat.Jpeg);
                img1 = Image.FromStream(stream1);

                //ado1.sel1("select ID,pic_ico from wp_pic where id=" + ID);
                //if (ado1.ds.Tables[0].Rows[0]["pic_ico"].ToString() == "")
                //{
                ado1.save_img("wp_pic", "WHERE ID=" + ID, "pic_Ico", bmp);
                //}

                //bmp.Save(topath, System.Drawing.Imaging.ImageFormat.Jpeg);

                bmp.Dispose();
                img.Dispose();
                grap.Dispose();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                CodeSite.Send(ex.ToString());
                CodeSite.Send("public void setIco(String ID)", ex);
            }
        }

        public void picEdt(string ID, Image img1)
        {
            string strSQL = "Where ID=" + ID;
            string tab = "wp_Pic";
            string col = "Pic";
            ado1.save_img(tab, strSQL, col, img1);
        }
    }
}
